package com.situ.user.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.situ.user.interceptor.CleanOrderInterceptor;
import com.situ.user.interceptor.UserInterceptor;

//相当于SpringMVC的配置文件
@Configuration
public class WebConfig implements WebMvcConfigurer {

	// 从配置文件中读取信息
	@Value("${spring.servlet.multipart.location}")
	private String path;

	@Autowired
	private UserInterceptor userInterceptor;

	@Autowired
	private CleanOrderInterceptor cleanOrderInterceptor;

	// 文件的上传路径的方法
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/upload/**").addResourceLocations("file:" + path);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// 配置无业务逻辑的路径与视图的映射
		registry.addViewController("/").setViewName("index");
	}

	// 拦截与放行
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(userInterceptor).addPathPatterns("/user/**").excludePathPatterns("/user/login",
				"/user/reg");
		registry.addInterceptor(cleanOrderInterceptor).addPathPatterns("/goods", "/user/cart/add", "/api/cart/**",
				"/user/order/add", "/api/order/getList");
	}
}
